<?php
Yii::import('Cms.components.widgets.BaseCmsWidget');
class FacebookTag extends BaseCmsWidget {
    
    public function run(){
        $models = $this->getActiveDataProvider()->getData();
        Yii::log(CVarDumper::dumpAsString($models), CLogger::LEVEL_ERROR);
        if (count($models))
        {
            $model = $models[0];
            if (!empty($model->title))
                Yii::app()->clientScript->registerMetaTag($model->title, 'og-title', null, array('property'=>'og:title'));
            if (!empty($model->type))
                Yii::app()->clientScript->registerMetaTag($model->type, 'og-type', null, array('property'=>'og:type'));
            if (empty($model->url))
                $url = Yii::app()->request->getUrl();
            else
                $url = trim(Yii::app()->request->getHostInfo(), '/').'/'.ltrim($model->url,'/');
            Yii::app()->clientScript->registerMetaTag($url, 'og-url', null, array('property'=>'og:url'));
            if (!empty($model->image))
                Yii::app()->clientScript->registerMetaTag($model->image, 'og-image', null, array('property'=>'og:image'));
            if (!empty($model->site_name))
                Yii::app()->clientScript->registerMetaTag($model->site_name, 'og-site_name', null, array('property'=>'og:site_name'));
            if (!empty($model->description))
                Yii::app()->clientScript->registerMetaTag($model->description, 'og-description', null, array('property'=>'og:description'));
        }
        return '';
    }
    
    // this widget does not have layout
    public function getSelectedLayout() {
        return null;
    }
    
    // return the url to edit text content
    public function getEditUrl($params = array()){
        $params['page_widget_id'] = $this->pageWidgetId;
        $params['id'] = $this->dataSettings['params'][':id'];
        
        return url('/Cms/widgets/facebookTag/update', $params);
    }
}
